#include "pch.hpp"

#include "Motylki.h"
#include "animation.h"

sf::Randomizer motyl_random;
animation motyl_anim;

void Motylki::load()
{
	motyl_random.SetSeed(time(0));

	int count = 5;
	
	Motyl m;

	while(count--)
	{
		m.angle = motyl_random.Random(0, 360);
		m.position.x = motyl_random.Random(-768/2.0f, 768/2.0f);
		m.position.y = motyl_random.Random(0.0f, 768.0f);
		m.random = motyl_random.Random(0, 123);
		m.random_anim_time = motyl_random.Random(1112,11124);

		motylki.push_back(m);
	}

	motyl_anim.load("data/motyl.png", 50, 50, 4, 33, true, 1.0f);
}

static float timer = 0.0f;

void Motylki::step(float maxx)
{
	timer += 1.0f/60.0f;
	for(int i=0; i<motylki.size(); i++)
	{
		motylki[i].angle += motyl_random.Random(-15.0f, 15.0f);
		motylki[i].position.x += sin(DEGTORAD(motylki[i].angle))*2;
		motylki[i].position.y += -cos(DEGTORAD(motylki[i].angle))*2;

		if(motylki[i].position.x < -maxx) { motylki[i].position.x = -maxx; motylki[i].angle += 180; }
		if(motylki[i].position.x > maxx) { motylki[i].position.x = maxx; motylki[i].angle += 180; }
		if(motylki[i].position.y < 0) { motylki[i].position.y = 0; motylki[i].angle += 180; }
		if(motylki[i].position.y > 768) { motylki[i].position.y = 768; motylki[i].angle += 180; }
	}
}

void Motylki::draw()
{
	for(int i=0; i<motylki.size(); i++)
	{
		motyl_anim.SetPos(motylki[i].position);
		sf::Vector2f dir(0, -1);
		sfm::Rotate(dir, motylki[i].angle);
		motyl_anim.draw(timer*1000+motylki[i].random, dir);
	}
}